//
//  CYBProductController.m
//  01-基本框架搭建
//
//  Created by apple on 16/9/2.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CYBProductController.h"
#import "CYBProject.h"
#import "CYBProductCell.h"


@interface CYBProductController ()

@property (nonatomic, strong) NSArray *projects;

@end

@implementation CYBProductController

static NSString *ID = @"cell";

- (NSArray *)projects{

    if (_projects == nil) {
        NSMutableArray *Marray = [NSMutableArray array];
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"project.json" ofType:nil]; //获取 json文件路径
        NSData *data = [NSData dataWithContentsOfFile:path]; //根据路径获取 data文件
        //根据传入的data获取字黄或数组
        NSArray *dictArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        for (NSDictionary *dict in dictArray) {
            CYBProject *p = [[CYBProject alloc]initWithDict:dict];    //对象方法
//            CYBProject *p = [CYBProject ProjectWithDict:dict];    //类方法
            [Marray addObject:p];
            
        }
        _projects = Marray;
        
        return _projects;
    }

    return _projects;
}

- (instancetype)init{
    
    //设置布局样式 为流水布局
    UICollectionViewFlowLayout *FlowLayout = [[UICollectionViewFlowLayout alloc] init];
    FlowLayout.itemSize = CGSizeMake(80, 80); //设置每个cell的大小
    FlowLayout.minimumLineSpacing = 20; //每排间隙
    FlowLayout.minimumInteritemSpacing = 20; //每列间隙
    FlowLayout.sectionInset = UIEdgeInsetsMake(FlowLayout.minimumLineSpacing, FlowLayout.minimumLineSpacing, FlowLayout.minimumLineSpacing, FlowLayout.minimumLineSpacing); //内边距
   
    return [super initWithCollectionViewLayout:FlowLayout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    //注册 cell 用代码创建时
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
    
    //注册cell用 nib时
     UINib *nib = [UINib nibWithNibName:@"CYBProductCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:ID];
    
  }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

//每组有多少个 cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.projects.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CYBProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    cell.project = self.projects[indexPath.item];
    
    cell.backgroundColor = [UIColor greenColor];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    CYBProject * p = self.projects[indexPath.item];

    NSLog(@"点击了-----%@",p.title);
}
@end
